﻿using HpxWMS_Api.CustomExcption;
using HpxWMS_Api.DAL;
using HpxWMS_Api.Entities;
using HpxWMS_Api.Models;
using HpxWMS_Api.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HpxWMS_Api.BLL
{
    /// <summary>
    /// 账户服务
    /// </summary>
    public class AccountManager
    {
        private AccountServices accountServices = new AccountServices();
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public UserDTO Login(Li li)
        {
            li.UserPwd = Md5Helper.MD5Encrypt16(li.UserPwd);
            var user = accountServices.FindUserNameandUserPwd(li.UserName, li.UserPwd);
            if (user == null)
            {
                return null;
            }
            return new UserDTO() { Id = user.Id, UserName = user.UserName, NickName = user.NickName };

        }
        /// <summary>
        /// 获取单个用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserDTO FindById(int id)
        {
            var user = accountServices.FindById(id);
            if (user == null)
            {
                return null;
            }
            return new UserDTO() { Id = user.Id, NickName = user.NickName, UserName = user.UserName };
        }
        /// <summary>
        /// 获取符合条件的数量
        /// </summary>
        /// <param name="searchText"></param>
        /// <returns></returns>
        public int Count(string searchText)
        {
            return accountServices.Count(searchText);
        }
        /// <summary>
        /// 获取一页数据
        /// </summary>
        /// <param name="uq"></param>
        public List<UserDTO> FindPage(Uq uq)
        {
            List<User> list = accountServices.FindPage(uq);
            return list.Select(user => new UserDTO() { UserName = user.UserName, Id = user.Id, NickName = user.NickName }).ToList();
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        public bool Insert(Ui ui)
        {
            //加密
            ui.UserPwd = Md5Helper.MD5Encrypt16(ui.UserPwd);
            if (accountServices.ExictByUserName(ui.UserName))
            {
                throw new BusinessException("009", "添加失败,用户名已存在");
            }
            return accountServices.InsertUser(ui);

        }
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="ue"></param>
        /// <returns></returns>
        public bool Update(Ue ue)
        {
            if (accountServices.ExictByUserNameandNotEqualId(ue.UserName, ue.Id))
            {
                throw new BusinessException("010", "更新失败,用户名已被使用");
            }
            return accountServices.UpdataUser(ue);

        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            return accountServices.Delete(id);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="cpwd"></param>
        /// <returns></returns>
        public bool ChangePwd(Cpwd cpwd)
        {
            //加密
            cpwd.UserPwd = Md5Helper.MD5Encrypt16(cpwd.UserPwd);
            if (accountServices.ExictByIdandNotEqualOldUserPwd(cpwd.UserPwd, cpwd.Id))
            {
                throw new BusinessException("011", "更新失败,新密码与旧密码相同");
            }
            return accountServices.ChangePwd(cpwd);

        }
    }
}